import extend from 'dva-model-extend'
import settings from '@/settings'
import model from '@/baseModel'
import { all_units, years, issue_task, issue_tasks } from '@/services/api'

const { pagination } = settings

export default extend(model, {
	namespace: 'issue',
	state: {
		units: [],
		subjects: [],
		years: []
	},
	effects: {
		*reset(_, {
			put
		}) {
			yield put({
				type: 'updateState',
				payload: {
					data: undefined,
					units: []
				}
			})
		},
		*fetchOne({
			payload
		}, {
			call,
			put
		}) {
			const { data } = yield call(issue_task, payload)
			yield put({
				type: 'updateState',
				payload: {
					data
				}
			})
		},
		*fetchAll({
			payload = pagination
		}, {
			call,
			put
		}) {
			const { data } = yield call(issue_tasks, payload)
			yield put({
				type: 'updateState',
				payload: {
					list: data,
					params: payload
				}
			})
		},
		*years(_, {
			call, put
		}) {
			const { data } = yield call(years, {
				type: 2
			})
			yield put({
				type: 'updateState',
				payload: {
					years: data
				}
			})
		},
		*fetchUnits({
			payload
		}, {
			call,
			put
		}) {
			const { data } = yield call(all_units, payload)
			yield put({
				type: 'updateState',
				payload: {
					units: data
				}
			})
		}
	}
})